package com.itheima.threaddemo9;

public class Ticket implements Runnable{
    // 票的数量
    private int ticket = 100;

    private static final Object OBJ = new Object();

    @Override
    public void run() {
        while (true) {
            // 多个线程必须使用同一把锁
            synchronized (OBJ) {
                if (ticket <= 0) {
                    break;
                } else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "在卖票，还剩下 " + --ticket + " 张票");
                }
            }
        }
    }
}
